package com.wdh.kjs.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wdh.kjs.MainActivity;
import com.wdh.kjs.R;
import com.wdh.kjs.http.Httpurl;
import com.wdh.kjs.model.JsonModel;
import com.wdh.kjs.model.JsonModel_my;
import com.wdh.kjs.util.ToastUtils;

/**
 * Created by feifei on 2016/4/28.
 */
public class FeedBackActivity extends BaseTopActivity implements View.OnClickListener {
    private Button bt_feedback;
    private EditText et_feedback;
    private Intent intent;
    private JsonModel jsonModel;
    private SharedPreferences sharedPreferences;
    public static String loginToken;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feedback);
        initview();
        sharedPreferences = getSharedPreferences("logininfo", MODE_PRIVATE);
        loginToken = sharedPreferences.getString("loginToken", "");
    }

    private void initview() {
        initTopBar("意见反馈");
        bt_feedback = (Button) findViewById(R.id.bt_feedback);
        et_feedback = (EditText) findViewById(R.id.et_feedback);
        bt_feedback.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_feedback:
                intweb();
        }
    }

    private void intweb() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("partyId", loginToken);
        params.addBodyParameter("source", "1");
        params.addBodyParameter("content", et_feedback.getText().toString());


        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_FEEDBACK, params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Gson gson = new Gson();
                jsonModel= gson.fromJson(responseInfo.result, JsonModel.class);
if (jsonModel.success) {
    trueDialogShow("反馈意见提交成功！");
    
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            intent = new Intent();
            intent.setClass(FeedBackActivity.this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
    }, 2000);

}
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                ToastUtils.showShort(getBaseContext(), "网络异常！");
            }
        });

    }
    private void trueDialogShow(String hint) {
        View dialog = (LinearLayout) getLayoutInflater().inflate(R.layout.view_dialog_true, null);
        TextView tv_dialog_false_hint = (TextView) dialog.findViewById(R.id.tv_dialog_true_hint);
        tv_dialog_false_hint.setText(hint);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialog);
        builder.create();
        final AlertDialog dialogShow = builder.show();

    }
}
